package com.lineage.server.templates;

import com.add.system.L1FireCrystal;
import com.add.system.L1FireSmithCrystalTable;
import com.lineage.DatabaseFactory;
import com.lineage.DatabaseFactoryLogin;
import com.lineage.data.cmd.CreateNewItem;
import com.lineage.data.event.GamblingSet;
import com.lineage.data.event.ShopXSet;
import com.lineage.data.event.gambling.GamblingNpc;
import com.lineage.data.npc.shop.Npc_ShopX;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.datatables.ShopTable;
import com.lineage.server.datatables.lock.DwarfShopReading;
import com.lineage.server.datatables.lock.GamblingReading;
import com.lineage.server.datatables.lock.IpReading;
import com.lineage.server.datatables.lock.ServerCnInfoReading;
import com.lineage.server.model.Instance.L1IllusoryInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.timecontroller.event.GamblingTime;
import com.lineage.server.utils.ListMapUtil;
import com.lineage.server.utils.RangeInt;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1PcOtherList {
  private static final Log _log = LogFactory.getLog(L1PcOtherList.class);
  
  private L1PcInstance _pc;
  
  public Map<Integer, L1ItemInstance> DELIST;
  
  private Map<Integer, L1ShopItem> _cnList;
  
  private Map<Integer, L1ItemInstance> _cnSList;
  
  private Map<Integer, GamblingNpc> _gamList;
  
  private Map<Integer, L1Gambling> _gamSellList;
  
  private Map<Integer, L1IllusoryInstance> _illusoryList;
  
  private Map<Integer, L1TeleportLoc> _teleport;
  
  private Map<Integer, Integer> _uplevelList;
  
  private Map<Integer, String[]> _shiftingList;
  
  private Map<Integer, L1ItemInstance> _sitemList;
  
  private Map<Integer, Integer> _sitemList2;
  
  public Map<Integer, L1Quest> QUESTMAP;
  
  public Map<Integer, L1ShopS> SHOPXMAP;
  
  public ArrayList<Integer> ATKNPC;
  
  private int[] _is;
  
  public Map<Integer, int[]> EZPAYLIST;
  
  public Map<Integer, int[]> SHOPLIST;
  
  public L1PcOtherList(L1PcInstance pc) {
    this._pc = pc;
    this.DELIST = new HashMap<>();
    this._cnList = new HashMap<>();
    this._cnSList = new HashMap<>();
    this._gamList = new HashMap<>();
    this._gamSellList = new HashMap<>();
    this._illusoryList = new HashMap<>();
    this._teleport = new HashMap<>();
    this._uplevelList = new HashMap<>();
    this._shiftingList = (Map)new HashMap<>();
    this._sitemList = new HashMap<>();
    this._sitemList2 = new HashMap<>();
    this.QUESTMAP = new HashMap<>();
    this.SHOPXMAP = new HashMap<>();
    this.ATKNPC = new ArrayList<>();
    this.EZPAYLIST = (Map)new HashMap<>();
    this.SHOPLIST = (Map)new HashMap<>();
  }
  
  public void clearAll() {
    try {
      ListMapUtil.clear(this.DELIST);
      ListMapUtil.clear(this._cnList);
      ListMapUtil.clear(this._cnSList);
      ListMapUtil.clear(this._gamList);
      ListMapUtil.clear(this._gamSellList);
      ListMapUtil.clear(this._illusoryList);
      ListMapUtil.clear(this._teleport);
      ListMapUtil.clear(this._uplevelList);
      ListMapUtil.clear(this._shiftingList);
      ListMapUtil.clear(this._sitemList);
      ListMapUtil.clear(this._sitemList2);
      ListMapUtil.clear(this.QUESTMAP);
      ListMapUtil.clear(this.SHOPXMAP);
      ListMapUtil.clear(this.ATKNPC);
      ListMapUtil.clear(this.EZPAYLIST);
      ListMapUtil.clear(this.SHOPLIST);
      this.DELIST = null;
      this._cnList = null;
      this._cnSList = null;
      this._gamList = null;
      this._gamSellList = null;
      this._illusoryList = null;
      this._teleport = null;
      this._uplevelList = null;
      this._shiftingList = null;
      this._sitemList = null;
      this._sitemList2 = null;
      this.QUESTMAP = null;
      this.SHOPXMAP = null;
      this.ATKNPC = null;
      this.EZPAYLIST = null;
      this.SHOPLIST = null;
      this._is = null;
      this._pc = null;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public Map<Integer, Integer> get_sitemList2() {
    return this._sitemList2;
  }
  
  public void add_sitemList2(Integer key, Integer value) {
    this._sitemList2.put(key, value);
  }
  
  public void clear_sitemList2() {
    this._sitemList2.clear();
  }
  
  public Map<Integer, L1ItemInstance> get_sitemList() {
    return this._sitemList;
  }
  
  public void add_sitemList(Integer key, L1ItemInstance value) {
    this._sitemList.put(key, value);
  }
  
  public void clear_sitemList() {
    this._sitemList.clear();
  }
  
  public Map<Integer, String[]> get_shiftingList() {
    return this._shiftingList;
  }
  
  public void add_shiftingList(Integer key, String[] value) {
    this._shiftingList.put(key, value);
  }
  
  public void remove_shiftingList(Integer key) {
    this._shiftingList.remove(key);
  }
  
  public void set_shiftingList() {
    try {
      this._shiftingList.clear();
      Connection conn = null;
      PreparedStatement pstm = null;
      ResultSet rs = null;
      try {
        conn = DatabaseFactory.get().getConnection();
        pstm = conn.prepareStatement("SELECT * FROM `characters` WHERE `account_name`=?");
        pstm.setString(1, this._pc.getAccountName());
        rs = pstm.executeQuery();
        int key = 0;
        while (rs.next()) {
          int objid = rs.getInt("objid");
          String name = rs.getString("char_name");
          if (!name.equalsIgnoreCase(this._pc.getName())) {
            key++;
            add_shiftingList(Integer.valueOf(key), new String[] { String.valueOf(objid), name });
          } 
        } 
      } catch (Exception e) {
        _log.error(e.getLocalizedMessage(), e);
      } finally {
        SQLUtil.close(rs);
        SQLUtil.close(pstm);
        SQLUtil.close(conn);
      } 
    } catch (Exception e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } 
  }
  
  public Map<Integer, L1IllusoryInstance> get_illusoryList() {
    return this._illusoryList;
  }
  
  public void addIllusoryList(Integer key, L1IllusoryInstance value) {
    this._illusoryList.put(key, value);
  }
  
  public void removeIllusoryList(Integer key) {
    try {
      if (this._illusoryList == null)
        return; 
      if (this._illusoryList.get(key) != null)
        this._illusoryList.remove(key); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void teleport(HashMap<Integer, L1TeleportLoc> teleportMap) {
    try {
      ListMapUtil.clear(this._teleport);
      this._teleport.putAll(teleportMap);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public Map<Integer, L1TeleportLoc> teleportMap() {
    return this._teleport;
  }
  
  public void sellall(Map<Integer, Integer> sellallMap) {
    try {
      int getprice = 0;
      int totalprice = 0;
      Iterator<Integer> iterator = sellallMap.keySet().iterator();
      while (iterator.hasNext()) {
        Integer integer = iterator.next();
        L1ItemInstance item = this._pc.getInventory().getItem(integer.intValue());
        if (item != null) {
          int key = item.getItemId();
          int price = ShopTable.get().getPrice(key);
          Integer count = sellallMap.get(integer);
          if (price >= 200000 && count.intValue() > 9999) {
            this._pc.sendPackets((ServerBasePacket)new S_SystemMessage("贩卖数量无法超过 9999个。"));
            return;
          } 
          totalprice += price * count.intValue();
          if (!RangeInt.includes(totalprice, 0, 2000000000)) {
            this._pc.sendPackets((ServerBasePacket)new S_SystemMessage("总共贩卖价格无法超过 20亿金币。"));
            return;
          } 
          long remove = this._pc.getInventory().removeItem(integer.intValue(), count.intValue());
          if (remove != count.intValue())
            continue; 
          getprice += price * count.intValue();
        } 
      } 
      if (getprice > 0) {
        L1ItemInstance item2 = ItemTable.get().createItem(40308);
        item2.setCount(getprice);
        createNewItem(item2);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void sellforfirecrystal(Map<Integer, Integer> FCMap) {
    try {
      int allcount = 0;
      Iterator<Integer> iterator = FCMap.keySet().iterator();
      while (iterator.hasNext()) {
        int objid = ((Integer)iterator.next()).intValue();
        L1ItemInstance item = this._pc.getInventory().getItem(objid);
        if (item != null) {
          int key = item.getItemId();
          if (item.getBless() == 0) {
            key = item.getItemId() - 100000;
          } else if (item.getBless() == 2) {
            key = item.getItemId() - 200000;
          } 
          L1FireCrystal firecrystal = L1FireSmithCrystalTable.get().getTemplate(key);
          int crystalcount = firecrystal.get_CrystalCount(item);
          int count = ((Integer)FCMap.get(Integer.valueOf(objid))).intValue();
          long remove = this._pc.getInventory().removeItem(objid, count);
          if (remove != count)
            continue; 
          allcount += crystalcount * count;
        } 
      } 
      if (allcount > 0) {
        L1ItemInstance item2 = ItemTable.get().createItem(80029);
        item2.setCount(allcount);
        createNewItem(item2);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void sellcnitem(Map<Integer, Integer> cnsellMap) {
    try {
      long adena_count = 0L;
      for (Integer integer : cnsellMap.keySet()) {
        int objid = integer.intValue();
        int count = ((Integer)cnsellMap.get(integer)).intValue();
        if (count > 0) {
          L1ItemInstance findItem = this._pc.getInventory().getItem(
              objid);
          if (findItem != null && findItem.getCount() >= count) {
            L1ShopItem element = this._cnList.get(
                Integer.valueOf(findItem.getId()));
            if (element.getPurchasingPrice() <= 0)
              continue; 
            if (element != null) {
              long adenaCount = this._pc.getInventory().removeItem(
                  findItem, count) * 
                element.getPurchasingPrice();
              adena_count += adenaCount;
              Yuanbaobuy("人物:" + this._pc.getName() + "售卖 +" + findItem.getName() + "(单价" + element.getPurchasingPrice() + ")" + "得到元宝(" + adenaCount + "),时间:" + "(" + 
                  new Timestamp(System.currentTimeMillis()) + ")。");
            } 
          } 
        } 
      } 
      if (adena_count > 0L)
        CreateNewItem.createNewItem(this._pc, this._pc.get_temp_adena(), adena_count); 
      this._pc.set_temp_adena(0);
      ListMapUtil.clear(this._cnList);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void Yuanbaobuy(String info) {
    try {
      BufferedWriter out = new BufferedWriter(new FileWriter("玩家纪录/玩家卖道具给元宝商店纪录.txt", true));
      out.write(String.valueOf(String.valueOf(info)) + "\r\n");
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    } 
  }
  
  public void clear() {
    try {
      ListMapUtil.clear(this._cnList);
      ListMapUtil.clear(this._gamList);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void set_gamSellList(Map<Integer, L1Gambling> sellList) {
    try {
      ListMapUtil.clear(this._gamSellList);
      this._gamSellList.putAll(sellList);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void get_sellGam(int objid, int count) {
    try {
      L1Gambling element = this._gamSellList.get(Integer.valueOf(objid));
      if (element == null)
        return; 
      long countx = (long)(element.get_rate() * GamblingSet.GAMADENA) * count;
      long remove = this._pc.getInventory().removeItem(objid, count);
      if (remove == count) {
        int outcount = element.get_outcount() - count;
        if (outcount < 0)
          return; 
        element.set_outcount(outcount);
        GamblingReading.get().updateGambling(element.get_id(), outcount);
        L1ItemInstance item = ItemTable.get().createItem(GamblingSet.ADENAITEM);
        item.setCount(countx);
        createNewItem(item);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void add_gamList(GamblingNpc element, int index) {
    this._gamList.put(new Integer(index), element);
  }
  
  public void get_buyGam(Map<Integer, Integer> gamMap) {
    try {
      Iterator<Integer> iterator = gamMap.keySet().iterator();
      while (iterator.hasNext()) {
        Integer integer = iterator.next();
        int index = integer.intValue();
        int count = ((Integer)gamMap.get(integer)).intValue();
        get_gamItem(index, count);
      } 
      ListMapUtil.clear(this._gamList);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void get_gamItem(int index, int count) {
    try {
      if (count <= 0)
        return; 
      GamblingNpc element = this._gamList.get(Integer.valueOf(index));
      if (element == null)
        return; 
      int npcid = element.get_npc().getNpcId();
      int no = GamblingTime.get_gamblingNo();
      long adena = (GamblingSet.GAMADENA * count);
      long srcCount = this._pc.getInventory().countItems(GamblingSet.ADENAITEM);
      if (srcCount >= adena) {
        L1ItemInstance item = ItemTable.get().createItem(40309);
        if (this._pc.getInventory().checkAddItem(item, count) == 0) {
          this._pc.getInventory().consumeItem(GamblingSet.ADENAITEM, adena);
          item.setCount(count);
          item.setraceGamNo(String.valueOf(String.valueOf(no)) + "-" + npcid);
          createNewItem(item);
          element.add_adena(adena);
        } else {
          this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(270));
        } 
      } else {
        L1Item item2 = ItemTable.get().getTemplate(GamblingSet.ADENAITEM);
        long nc = adena - srcCount;
        this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(item2.getNameId())) + "(" + nc + ")"));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void add_cnSList(L1ItemInstance shopItem, int index) {
    this._cnSList.put(new Integer(index), shopItem);
  }
  
  
  
	/**
	 * 买入托售管理员物品
	 */
	public void get_buyCnS(final Map<Integer, Integer> cnMap) {
		try {
			//final int itemid_cn = 40308;// 天币 40308
			for (final Integer integer : cnMap.keySet()) {
				final int count = cnMap.get(integer);
				if (count > 0) {
					// 取回卖出视窗对应排序编号物品
					final L1ItemInstance element = this._cnSList.get(integer.intValue());
					final L1ShopS shopS = DwarfShopReading.get().getShopS(element.getId());
					if (element != null && shopS != null) {
						if (shopS.get_end() != 0) {// 物品非出售中
							continue;
						}
						if (shopS.get_item() == null) {// 物品设置为空
							continue;
						}
						// 取回天币数量
						//final L1ItemInstance itemT = _pc.getInventory().checkItemX(itemid_cn, shopS.get_adena());
						final L1ItemInstance itemT = _pc.getInventory().checkItemX(ShopXSet.ITEMID, shopS.get_adena());
						if (itemT == null) {
							// 337：\f1%0不足%s。 0_o"
							//_pc.sendPackets(new S_ServerMessage(337, "天币"));
			                final L1Item shopXcash = ItemTable.get().getTemplate(ShopXSet.ITEMID);
							_pc.sendPackets(new S_ServerMessage(337, shopXcash.getName()));
							continue;
						}

						shopS.set_end(1);// 设置资讯为售出
						shopS.set_item(null);
						shopS.set_buy_objid(_pc.getId());
						shopS.set_buy_name(_pc.getName());
						DwarfShopReading.get().updateShopS(shopS);
						DwarfShopReading.get().deleteItem(element.getId());

						//this._pc.getInventory().consumeItem(itemid_cn, shopS.get_adena());
						this._pc.getInventory().consumeItem(ShopXSet.ITEMID, shopS.get_adena());
						this._pc.getInventory().storeTradeItem(element);
						this._pc.sendPackets(new S_ServerMessage(403, element.getLogName())); // 获得0%。
						// createNewItem(element);
					}
				}
			}
			ListMapUtil.clear(this._cnList);

		} catch (final Exception e) {
			_log.error(e.getLocalizedMessage(), e);
		}
	}
  
//  public void get_buyCnS(Map<Integer, Integer> cnMap) {
//    try {
//      int itemid_cn = Npc_ShopX._itemid;
//      Iterator<Integer> iterator = cnMap.keySet().iterator();
//      while (iterator.hasNext()) {
//        Integer integer = iterator.next();
//        int count = ((Integer)cnMap.get(integer)).intValue();
//        if (count > 0) {
//          L1ItemInstance element = this._cnSList.get(Integer.valueOf(integer.intValue()));
//          L1ShopS shopS = DwarfShopReading.get().getShopS(element.getId());
//          if (element == null || shopS == null)
//            continue; 
//          if (shopS.get_end() != 0)
//            continue; 
//          if (shopS.get_item() == null)
//            continue; 
//          L1ItemInstance itemT = this._pc.getInventory().checkItemX(itemid_cn, shopS.get_adena());
//          if (itemT == null) {
//            this._pc.sendPackets(
//                (ServerBasePacket)new S_ServerMessage(337, ItemTable.get().getTemplate(Npc_ShopX._itemid).getName()));
//            continue;
//          } 
//          shopS.set_end(1);
//          shopS.set_item(null);
//          DwarfShopReading.get().updateShopS(shopS);
//          DwarfShopReading.get().deleteItem(element.getId());
//          this._pc.getInventory().consumeItem(itemid_cn, shopS.get_adena());
//          this._pc.getInventory().storeTradeItem(element);
//          this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, element.getLogName()));
//        } 
//      } 
//      ListMapUtil.clear(this._cnList);
//    } catch (Exception e) {
//      _log.error(e.getLocalizedMessage(), e);
//    } 
//  }
//  
  public void add_cnList(int index, L1ShopItem shopItem) {
    this._cnList.put(Integer.valueOf(index), shopItem);
  }
  
  public void get_buyCn(Map<Integer, Integer> cnMap) {
    try {
      if (this._pc.getInventory().getSize() + cnMap.size() >= 170) {
        this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(263));
      } else {
        Iterator<Integer> iterator = cnMap.keySet().iterator();
        while (iterator.hasNext()) {
          int index = ((Integer)iterator.next()).intValue();
          int count = ((Integer)cnMap.get(Integer.valueOf(index))).intValue();
          if (count > 0) {
            L1ShopItem element = this._cnList.get(Integer.valueOf(index));
            if (element == null)
              continue; 
            get_cnItem(element, count);
          } 
        } 
      } 
      this._pc.set_temp_adena(0);
      ListMapUtil.clear(this._cnList);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void get_cnItem(L1ShopItem element, int count) {
    try {
      int itemid_cn = this._pc.get_temp_adena();
      int itemid = element.getItemId();
      int packcount = element.getPackCount();
      int getCount = count;
      if (packcount > 1)
        getCount = element.getPackCount() * count; 
      int enchantlevel = element.getEnchantLevel();
      int adenaCount = element.getPrice() * count;
      int DailyBuyingCount = element.getDailyBuyingCount();
      if (this._pc.getInventory().checkAddItem(element.getItem(), getCount) != 0)
        return; 
      if (adenaCount <= 0) {
        IpReading.get().add(this._pc.getAccountName(), "负数洗物:" + adenaCount);
        this._pc.getNetConnection().kick();
        return;
      } 
      long srcCount = this._pc.getInventory().countItems(itemid_cn);
      if (srcCount >= adenaCount && this._pc.getInventory().checkItem(itemid_cn, adenaCount)) {
        L1Item itemtmp = ItemTable.get().getTemplate(itemid);
        if (!itemtmp.isStackable() && this._pc.getInventory().getSize() + getCount >= 170) {
          this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(263));
          return;
        } 
        if (itemtmp.isStackable()) {
          if (DailyBuyingCount > 0) {
            int AlreadyBoughtCount = this._pc.getQuest().get_step(itemid);
            int buyingcount = getCount;
            if (buyingcount > DailyBuyingCount) {
              this._pc.sendPackets((ServerBasePacket)new S_ServerMessage("超过每日限制购买数量上限"));
              return;
            } 
            if (AlreadyBoughtCount >= DailyBuyingCount || 
              buyingcount + AlreadyBoughtCount > DailyBuyingCount) {
              this._pc.sendPackets((ServerBasePacket)new S_ServerMessage("超过每日限制购买数量上限。"));
              return;
            } 
            this._pc.getQuest().set_step(itemid, buyingcount + AlreadyBoughtCount);
          } 
          L1ItemInstance item = ItemTable.get().createItem(itemid);
          item.setCount(getCount);
          item.setEnchantLevel(enchantlevel);
          item.setIdentified(true);
          bonusCheck(adenaCount);
          bonusCheck1(adenaCount);
          createNewItem(item);
          toGmMsg(itemtmp, adenaCount, true, itemid_cn);
          L1Item temp = ItemTable.get().getTemplate(itemid_cn);
          RecordTable.get().recordeShop1(this._pc.getName(), temp.getName(), adenaCount, item.getAllName(), 
              getCount, item.getId(), this._pc.getIp());
        } else {
          if (DailyBuyingCount > 0) {
            int AlreadyBoughtCount = this._pc.getQuest().get_step(itemid);
            int buyingcount = getCount;
            if (buyingcount > DailyBuyingCount) {
              this._pc.sendPackets((ServerBasePacket)new S_ServerMessage("超过每日限制购买数量上限"));
              return;
            } 
            if (AlreadyBoughtCount >= DailyBuyingCount || 
              buyingcount + AlreadyBoughtCount > DailyBuyingCount) {
              this._pc.sendPackets((ServerBasePacket)new S_ServerMessage("超过每日限制购买数量上限。"));
              return;
            } 
            this._pc.getQuest().set_step(itemid, buyingcount + AlreadyBoughtCount);
          } 
          int i = 0;
          while (i < getCount) {
            L1ItemInstance item2 = ItemTable.get().createItem(itemid);
            item2.setEnchantLevel(enchantlevel);
            item2.setIdentified(true);
            bonusCheck(adenaCount);
            createNewItem(item2);
            toGmMsg(itemtmp, adenaCount, true, itemid_cn);
            L1Item temp2 = ItemTable.get().getTemplate(itemid_cn);
            RecordTable.get().recordeShop1(this._pc.getName(), temp2.getName(), adenaCount, 
                item2.getAllName(), getCount, item2.getId(), this._pc.getIp());
            i++;
          } 
        } 
        this._pc.getInventory().consumeItem(itemid_cn, adenaCount);
      } else {
        long nc = adenaCount - srcCount;
        L1Item item3 = ItemTable.get().getTemplate(itemid_cn);
        this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(item3.getName())) + "(" + nc + ")"));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void toGmMsg(L1Item itemtmp, int adenaCount, boolean mode, int itemid_cn) {
    try {
      ServerCnInfoReading.get().create(this._pc, itemtmp, adenaCount, mode, itemid_cn);
      Collection<L1PcInstance> allPc = World.get().getAllPlayers();
      Iterator<L1PcInstance> iterator = allPc.iterator();
      while (iterator.hasNext()) {
        L1PcInstance tgpc = iterator.next();
        if (tgpc.isGm()) {
          StringBuilder topc = new StringBuilder();
          if (mode) {
            topc.append("人物:" + this._pc.getName() + " 买入:" + itemtmp.getName() + " 花费商城币:" + adenaCount);
            tgpc.sendPackets((ServerBasePacket)new S_ServerMessage(166, topc.toString()));
            continue;
          } 
          topc.append("人物:" + this._pc.getName() + " 卖出:" + itemtmp.getName() + " 获得商城币:" + adenaCount);
          tgpc.sendPackets((ServerBasePacket)new S_ServerMessage(166, topc.toString()));
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void createNewItem(L1ItemInstance item) {
    try {
      this._pc.getInventory().storeItem(item);
      this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void add_levelList(int key, int value) {
    this._uplevelList.put(Integer.valueOf(key), Integer.valueOf(value));
  }
  
  public Map<Integer, Integer> get_uplevelList() {
    return this._uplevelList;
  }
  
  public Integer get_uplevelList(int key) {
    return this._uplevelList.get(Integer.valueOf(key));
  }
  
  public void clear_uplevelList() {
    ListMapUtil.clear(this._uplevelList);
  }
  
  public void set_newPcOriginal(int[] is) {
    this._is = is;
  }
  
  public int[] get_newPcOriginal() {
    return this._is;
  }
  
  private void bonusCheck(long count) {
    if (count == 0L)
      return; 
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "SELECT * FROM `shop_商城消费赠礼` ORDER BY `商城消费赠礼` DESC";
      ps = co.prepareStatement("SELECT * FROM `shop_商城消费赠礼` ORDER BY `商城消费赠礼` DESC");
      rs = ps.executeQuery();
      Timestamp now_time = new Timestamp(System.currentTimeMillis());
      while (rs.next()) {
        Timestamp end_time = rs.getTimestamp("限制结束时间");
        if (end_time != null && now_time.after(end_time))
          continue; 
        int need_counts = rs.getInt("商城消费赠礼");
        if (count < need_counts)
          continue; 
        int item_id = rs.getInt("道具编号");
        int counts = rs.getInt("道具数量");
        L1ItemInstance item = ItemTable.get().createItem(item_id);
        if (item == null) {
          _log.error("给予物件失败 原因: 指定编号物品不存在(" + item_id + ")");
          break;
        } 
        if (item.isStackable()) {
          item.setCount(counts);
          item.setIdentified(true);
          this._pc.getInventory().storeItem(item);
        } else {
          item.setIdentified(true);
          this._pc.getInventory().storeItem(item);
        } 
        this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
        break;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
      SQLUtil.close(rs);
    } 
  }
  
  private void bonusCheck1(long count) {
    if (count == 0L)
      return; 
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "SELECT * FROM `shop_商城一比一回馈` ORDER BY `道具编号` DESC";
      ps = co.prepareStatement("SELECT * FROM `shop_商城一比一回馈` ORDER BY `道具编号` DESC");
      rs = ps.executeQuery();
      Timestamp now_time = new Timestamp(System.currentTimeMillis());
      while (rs.next()) {
        Timestamp end_time = rs.getTimestamp("限制结束时间");
        if (end_time != null && now_time.after(end_time))
          continue; 
        int item_id = rs.getInt("道具编号");
        L1ItemInstance item = ItemTable.get().createItem(item_id);
        if (item == null) {
          _log.error("给予物件失败 原因: 指定编号物品不存在(" + item_id + ")");
          break;
        } 
        if (item.isStackable()) {
          item.setCount(count);
          item.setIdentified(true);
          this._pc.getInventory().storeItem(item);
        } else {
          item.setIdentified(true);
          this._pc.getInventory().storeItem(item);
        } 
        this._pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
        break;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
      SQLUtil.close(rs);
    } 
  }
  
  public void add_cnList(L1ShopItem shopItem, int index) {
    this._cnList.put(new Integer(index), shopItem);
  }
}
